/**
 *   eHOT. Desktop Java application allows to create notification messages,
 *   reminders, schedule your tasks and simply manage your time.
 *   Copyright (C) 2011  Anastasia Kolesnikova, Artem Perepelitsa
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ehot.persistence;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//import java.io.ObjectInputStream;
//import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import net.sf.beanlib.utils.xml.XStreamFactory;

import org.apache.log4j.Logger;

import com.thoughtworks.xstream.XStream;

public class NotesContainer implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4592182493089633327L;
	private static Logger logger = Logger.getLogger(NotesContainer.class);
			
	private Map<Long, INote> noteList;
	private long nextNoteNumber = 0;
	
	private static NotesContainer instance = null;

	private NotesContainer() {
		noteList = new HashMap<Long, INote>();

		{
			try{
/*				FileInputStream file = new FileInputStream(AbstractNote.getSerializationFileName());
				ObjectInputStream in = new ObjectInputStream(file);
				
				instance = (NotesContainer)in.readObject();
				in.close();
*/				
				XStream xstream = XStreamFactory.getInstance().getXStream();
				xstream.alias("NotesContainer", NotesContainer.class);
				xstream.alias("SimpleNote", SimpleNote.class);
				
				FileInputStream xmlStorage = new FileInputStream("notes.xml");
				instance = (NotesContainer)xstream.fromXML(xmlStorage);
				
				//System.out.println(xstream.toXML(instance));
				
				logger.trace("Deserialization completed");
			}
			catch(FileNotFoundException exception){
				logger.error(exception.getMessage(), exception);
			}
			/*catch(IOException exception){
				logger.error(exception.getMessage(), exception);
			}
			catch (ClassNotFoundException exception){
				logger.error(exception.getMessage(), exception);
			}*/
		}
	}
	
	public static NotesContainer getInstance(){
		if (null == instance){
		   new NotesContainer();
		}
		return instance;
	}

	public void addNote(SimpleNote aNote) {
		noteList.put(aNote.getId(), aNote);
	}

	public void addNote(String noteText) {
		SimpleNote newNote = new SimpleNote();
		newNote.setId(nextNoteNumber++);
		newNote.setDescription(noteText);
		noteList.put(newNote.getId(), newNote);
	}

	public boolean removeNote(Long id) {
		boolean bRet = false;
		if (noteList.containsKey(id - 1)) {
			noteList.remove(id - 1);
			bRet = true;
		}
		return bRet;
	}

	public INote getNote(Long id) {
		return noteList.get(id - 1);
	}

	public boolean containsNote(Long id) {
		return noteList.containsKey(id - 1);
	}

	public String getNoteText(Long id) {
		return this.getNote(id).getDescription();
	}

	public void setNoteText(Long id, String noteText) {
		this.getNote(id).setDescription(noteText);
	}

	public Collection<INote> values() {
		return noteList.values();
	}

	public void save() {
		try{
/*			FileOutputStream file = new FileOutputStream(AbstractNote.getSerializationFileName());
			ObjectOutputStream out = new ObjectOutputStream(file);
			out.writeObject(this);
			out.close();
*/			
			XStream xstream = XStreamFactory.getInstance().getXStream();
			xstream.alias("NotesContainer", NotesContainer.class);
			xstream.alias("SimpleNote", SimpleNote.class);
			
			FileOutputStream xmlStorage = new FileOutputStream("notes.xml");
			DataOutputStream xmlOut = new DataOutputStream(xmlStorage);

			xstream.toXML(this, xmlOut);
			xmlOut.writeUTF(xstream.toXML(instance));

			//System.out.println(xstream.toXML(this));
			
			logger.trace("Serialization completed");
		}
		catch(FileNotFoundException exception){
			logger.error(exception.getMessage(), exception);
		}
		catch(IOException exception){
			logger.error(exception.getMessage(), exception);
		}
	}
}
